<template>
  <view>
    <uni-list>
      <uni-list-item>
        <view slot="header" class="slot-box">
          <view class="header-text">我配置的</view>
        </view>
        <template slot="footer">
          <view class="footer-add-container" @tap="toStaffing">
            <image
              class="slot-image"
              src="../../static/add.png"
              mode="widthFix"
            ></image>
            <text>添加人员配置</text>
          </view>
        </template>
      </uni-list-item>
      <block v-for="(item, index) in staff" :key="index">
        <uni-list-item
          :thumb="item.userImage"
          thumb-size="lg"
          :title="item.userName"
          link
          @click="toScopeAuthority(item)"
        ></uni-list-item>
      </block>
    </uni-list>
    <view class="null-content"></view>
    <view class="footer-btn">
      <view class="to-add" @tap="toAuthority">我的权限</view>
      <view class="to-add to-save" @tap="toManage">我管理的</view>
    </view>
  </view>
</template>

<script>
import uniList from '../../components/uni-list/uni-list';
import uniListItem from '../../components/uni-list-item/uni-list-item';
import uniIcons from '../../components/uni-icons/uni-icons';
import { getMyPowerConfig } from '../../common/service.js';
export default {
  components: {
    uniList,
    uniListItem,
    uniIcons,
  },
  data() {
    return {
      loginInfo: {
        parentUserId: '',
        powerModule: '',
        toPowerId: '',
      },
      staff: [],
    };
  },
  onLoad: function (e) {
    if (!(this.CONFIG.user && this.CONFIG.user.id)) {
      uni.showToast({
        title: '当前认证的用户没找到',
        duration: 2000,
      });
      return;
    } else {
      this.loginInfo.parentUserId = this.CONFIG.user.id;
      if (e.powerModule) {
        this.loginInfo.powerModule = e.powerModule;
      } else {
        this.loginInfo.powerModule = 'XD_RESULT';
      }
    }
  },
  onShow() {
    if (this.loginInfo.parentUserId) {
      this.getMyPowerConfig();
    }
  },
  methods: {
    getMyPowerConfig() {
      uni.showLoading({
        title: '加载中',
      });
      getMyPowerConfig(
        {
          parentUserId: this.loginInfo.parentUserId,
          powerModule: this.loginInfo.powerModule,
        },
        (res) => {
          uni.hideLoading();
          if (res && res.data && res.data.code == 200) {
            if (res.data.data) {
              let list = res.data.data;
              let arr = [];
              for (let i = 0; i < list.length; i++) {
                arr.push(this.__format(list[i]));
              }
              this.staff = arr;
            } else {
              this.staff = [];
            }
          } else {
            uni.showToast({
              title: '获取我配置的权限失败',
              duration: 2000,
            });
          }
        },
        (err) => {
          uni.showToast({
            title: '获取我配置的权限失败',
            duration: 2000,
          });
        }
      );
    },
    toAuthority() {
      uni.navigateTo({
        url:
          '../myPermissions/myPermissions?loginInfo=' +
          JSON.stringify(this.loginInfo),
      });
    },
    toScopeAuthority(item) {
      this.loginInfo.toPowerId = item.userId;
      uni.navigateTo({
        url:
          '../scopeAuthority/scopeAuthority?loginInfo=' +
          JSON.stringify(this.loginInfo),
      });
    },
    toManage() {
      uni.navigateTo({
        url:
          '../myManagement/myManagement?loginInfo=' +
          JSON.stringify(this.loginInfo),
      });
    },
    toStaffing() {
      uni.navigateTo({
        url: '../staffing/staffing?loginInfo=' + JSON.stringify(this.loginInfo),
      });
    },
    // 格式转化
    __format(item) {
      return {
        userId: item.userName,
        userImage: item.avatar,
        userName: item.nickName,
        type: 'employee',
        checked: false,
      };
    },
  },
};
</script>

<style scope>
.header-text {
  display: flex;
  align-items: center;
  font-size: 32upx;
  font-weight: bold;
  color: #333333;
  height: 100%;
}
.footer-add-container {
  padding: 15upx;
  background: #ddecff;
  border-radius: 6px;
}
.slot-image {
  margin-right: 10upx;
  width: 24upx;
  height: 24upx;
}
.footer-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
}
.to-add {
  flex: 1;
  line-height: 100rpx;
  text-align: center;
  font-size: 30rpx;
  font-weight: bold;
  color: #ffffff;
  background: #35c260;
  border-radius: 6upx;
  margin: 30upx 20upx;
}
.to-save {
  background: #3875c6;
}
.null-content {
  height: 160upx;
}
</style>
